package rsps

import (
	"github.com/gin-gonic/gin"
	"local.public/errs"
	"local.public/utils"
	"log"
	"reflect"
	"strconv"
)

var AppName = ""

type ModelTableNameStu interface {
	TableName() string
}

func GetTableName(pModel interface{}) string {
	sTableName := ""
	if _, ok := pModel.(ModelTableNameStu); ok {
		sTableName = pModel.(ModelTableNameStu).TableName()
	} else {
		//typ =  reflect.TypeOf(pModel)
		//kd
		sTableName = reflect.TypeOf(pModel).Name()
	}
	return sTableName
}

// ResponseInfo 响应数据模板
type ResponseInfo struct {
	Success       bool        `json:"success"`
	Code          int         `json:"code"`
	Data          interface{} `json:"data,omitempty"`
	Message       string      `json:"msg"`        //打印的Msg可用于前端页面显示
	MessageDetail string      `json:"msg_detail"` //打印的Msg可用于Log显示，一般打印具体错误信息

	From  string `json:"from"`
	Table string `json:"table,omitempty"`
}

// SuccessRspWithData 成功的响应，带数据
func SuccessRspWithData(ctx *gin.Context, data interface{}) {
	ctx.JSON(200, ResponseInfo{
		Success: true,
		Code:    200,
		Message: "success",
		From:    AppName,
		Data:    data,
	})

}
func FailedRspAsCodeError(ctx *gin.Context, err error) {
	if err == nil {
		ctx.JSON(200, ResponseInfo{
			Success:       false,
			Code:          417,
			Message:       err.Error(),
			MessageDetail: err.Error(),
			From:          AppName,
		})
	}
	s := "[FailedRspWithError] " + err.Error()
	log.Output(2, s)

	switch typed := err.(type) {
	case *errs.CodeError:
		ctx.JSON(200, ResponseInfo{
			Success:       false,
			Code:          typed.Code,
			Message:       typed.MsgShow,
			MessageDetail: typed.ErrMsg,
			From:          AppName,
		})
	case error:
		ctx.JSON(200, ResponseInfo{
			Success:       false,
			Code:          417,
			Message:       typed.Error(),
			MessageDetail: "",
			From:          AppName,
		})
	}
}

func FailedRspWithError(ctx *gin.Context, msg string, err error) {
	if err == nil {
		ctx.JSON(200, ResponseInfo{
			Success:       false,
			Code:          417,
			Message:       msg,
			MessageDetail: err.Error(),
			From:          AppName,
		})
	}
	s := "[FailedRspWithError] " + err.Error()
	log.Output(2, s)

	switch typed := err.(type) {
	case *errs.CodeError:
		ctx.JSON(200, ResponseInfo{
			Success:       false,
			Code:          typed.Code,
			Message:       utils.IfEmpty(msg, err.Error()),
			MessageDetail: typed.Error(),
			From:          AppName,
		})
	case error:
		ctx.JSON(200, ResponseInfo{
			Success:       false,
			Code:          417,
			Message:       msg,
			MessageDetail: typed.Error(),
			From:          AppName,
		})
	}
}

func FailedRspWithCodeMsg(ctx *gin.Context, code int, msg string, errMsg string) {
	var iCode int
	iCode = code
	if code == 0 || code/100 == 2 {
		iCode = 400
	}

	s := "[FailedRspWithCodeMsg] " + "code:" + strconv.Itoa(code) + " msg:" + errMsg
	log.Output(2, s)

	ctx.JSON(200, ResponseInfo{
		Success:       false,
		Code:          iCode,
		Message:       msg,
		MessageDetail: errMsg,
		From:          AppName,
	})
}

func FailedRspWithCodeData(ctx *gin.Context, code int, msg string, data interface{}) {
	var iCode int
	iCode = code
	if code == 0 || code/100 == 2 {
		iCode = 400
	}

	s := "[FailedRspWithCodeMsg] " + "code:" + strconv.Itoa(code)
	log.Output(2, s)

	ctx.JSON(200, ResponseInfo{
		Success:       false,
		Code:          iCode,
		Message:       msg,
		MessageDetail: msg,
		Data:          data,
		From:          AppName,
	})
}

func FailedRspWith417Msg(ctx *gin.Context, message string) {
	s := "[Error] " + message
	log.Output(2, s)

	ctx.JSON(200, ResponseInfo{
		Success: false,
		Code:    417,
		Message: message,
		From:    AppName,
	})
}

func FailedRspWith416Msg(ctx *gin.Context, pModel interface{}, message string, errmsg string) {
	s := "[Param Error] " + message
	log.Output(2, s)
	sTableName := ""
	if pModel != nil {
		sTableName = GetTableName(pModel)
	}
	ctx.JSON(200, ResponseInfo{
		Success:       false,
		Code:          416,
		Message:       message,
		MessageDetail: errmsg,
		Table:         sTableName,
		From:          AppName,
	})
}
